package cn.exrick.xboot.modules.masterdata.server;

import cn.exrick.xboot.modules.masterdata.entity.dto.MasterUserDto;
import cn.exrick.xboot.modules.masterdata.server.call.CallMasterGetData;
import cn.exrick.xboot.modules.masterdata.server.call.CallMasterSendData;
import cn.exrick.xboot.modules.masterdata.server.servers.MasterUserService;
import cn.exrick.xboot.modules.masterdata.server.utils.Dom4JforXML;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 实现主数据的增量更新
 */

@Slf4j
@Service
public class MasterDataUpdate {
    @Autowired
    private CallMasterGetData callMasterGetData;
    @Autowired
    private CallMasterSendData callMasterSendData;
    @Autowired
    private MasterUserService masterUserService;

    // 1.调用getData(）接口获取增量数据
    // 2.将获取的数据转换成MasterUserDto对象
    // 3.将获取到的增量数据存储到本地数据库
    // 4.生成调用sendDataStates()接口的请求报文
    // 5.调用sendDataStates()接口

    public void dataUpdate(){
        log.info("开始主数据的增量更新,时间："+DateUtil.now() );
        //1.调用getData(）接口获取增量数据
        String data = callMasterGetData.getData();
      /*  data="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\t<sendData>\n" +
                "\t\t<totalRecord>1139</totalRecord>\n" +
                "\t\t<totalPage>6</totalPage>\n" +
                "\t\t<pageSize>200</pageSize>\n" +
                "\t\t<currentPage>1</currentPage>\n" +
                "\t\t<taskId>7DB30B4FA31C9E2CB2834615FAB987FB</taskId>\n" +
                "\t\t<datas>\n" +
                "\t\t\t<data tableName=\"MD_MIDSTAFF\">\n" +
                "\t\t\t\t<createUnit>--</createUnit>\n" +
                "\t\t\t\t<recid>358DBF39A1CBA1ADCDB0A70FEC503D5C</recid>\n" +
                "\t\t\t\t<version>8003606935628677633</version>\n" +
                "\t\t\t\t<field name=\"STDCODE\" type=\"2\" value=\"430104195907111520\"/>\n" +
                "\t\t\t\t<field name=\"STDNAME\" type=\"2\" value=\"黄汉莲停用try\"/>\n" +
                "\t\t\t\t<field name=\"A0177\" type=\"2\" value=\"430104195907111520\"/>\n" +
                "\t\t\t\t<field name=\"FLAG\" type=\"2\" value=\"3\"/>\n" +
                "\t\t\t\t<field name=\"A0148\" type=\"2\" value=\"13627423398\"/>\n" +
                "\t\t\t\t<field name=\"LEVEL\" type=\"3\" value=\"1\"/>\n" +
                "\t\t\t\t<field name=\"ISLEAF\" type=\"4\" value=\"true\"/>\n" +
                "\t\t\t\t<field name=\"SORTORDER\" type=\"1\" value=\"13301.0\"/>\n" +
                "\t\t\t\t<field name=\"INVALIDTIME\" type=\"6\" value=\"9999-12-31 00:00:00\"/>\n" +
                "\t\t\t\t<field name=\"VALIDTIME\" type=\"6\" value=\"1990-01-01 00:00:00\"/>\n" +
                "\t\t\t\t<field name=\"PARENTS\" type=\"10\"/>\n" +
                "\t\t\t</data>\n" +
                "\t\t\t</datas>\n" +
                "\t</sendData>";*/
        log.info("getData()接口返回报文："+data);
        //2.将获取的数据转换成MasterUserDto对象
        MasterUserDto masterUserDto = Dom4JforXML.xmlToMasterUserDto(data);
        log.info("集合数据,masterUserDto{}"+masterUserDto);
        // 3.TODO 将获取到的增量存储到数据库中
        masterUserDto = masterUserService.saveUser(masterUserDto);
        //4.生成调用sendDataStates()接口的请求报文
        String sendXml = Dom4JforXML.getSendXml(masterUserDto);
        log.info("请求报文,sendXml{}"+sendXml);
        //5.调用sendDataStates()接口
        String s = callMasterSendData.sendDataStates(sendXml);
        log.info("更新数据的返回报文：{}"+s);
        log.info("结束主数据的增量更新,时间："+DateUtil.now() );
    }


    public static void main(String[] args) {
          String data="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\t<sendData>\n" +
                "\t\t<totalRecord>1139</totalRecord>\n" +
                "\t\t<totalPage>6</totalPage>\n" +
                "\t\t<pageSize>200</pageSize>\n" +
                "\t\t<currentPage>1</currentPage>\n" +
                "\t\t<taskId>7DB30B4FA31C9E2CB2834615FAB987FB</taskId>\n" +
                "\t\t<datas>\n" +
                "\t\t\t<data tableName=\"MD_MIDSTAFF\">\n" +
                "\t\t\t\t<createUnit>--</createUnit>\n" +
                "\t\t\t\t<recid>358DBF39A1CBA1ADCDB0A70FEC503D5C</recid>\n" +
                "\t\t\t\t<version>8003606935628677633</version>\n" +
                "\t\t\t\t<field name=\"STDCODE\" type=\"2\" value=\"430104195907111520\"/>\n" +
                "\t\t\t\t<field name=\"STDNAME\" type=\"2\" value=\"黄汉莲停用try\"/>\n" +
                "\t\t\t\t<field name=\"A0177\" type=\"2\" value=\"430104195907111520\"/>\n" +
                "\t\t\t\t<field name=\"FLAG\" type=\"2\" value=\"3\"/>\n" +
                "\t\t\t\t<field name=\"A0148\" type=\"2\" value=\"13627423398\"/>\n" +
                "\t\t\t\t<field name=\"LEVEL\" type=\"3\" value=\"1\"/>\n" +
                "\t\t\t\t<field name=\"ISLEAF\" type=\"4\" value=\"true\"/>\n" +
                "\t\t\t\t<field name=\"SORTORDER\" type=\"1\" value=\"13301.0\"/>\n" +
                "\t\t\t\t<field name=\"INVALIDTIME\" type=\"6\" value=\"9999-12-31 00:00:00\"/>\n" +
                "\t\t\t\t<field name=\"VALIDTIME\" type=\"6\" value=\"1990-01-01 00:00:00\"/>\n" +
                "\t\t\t\t<field name=\"PARENTS\" type=\"10\"/>\n" +
                "\t\t\t</data>\n" +
                "\t\t\t</datas>\n" +
                "\t</sendData>";
        System.out.println(data);
    }
}
